#! /usr/bin/python3

import os
import requests
import json

if __name__ == '__main__':

    repository_name = input("请输入想要创建的仓库名:\n")
    fileName = "access_token.txt"
    if os.path.exists(fileName):
        with open(fileName, 'r', encoding='utf-8') as f:
            access_token = f.read().strip()
    else:
        access_token = input("请输入token:\n")

    url = "https://gitee.com/api/v5/user/repos"
    header = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    request_body = '{"access_token":"%s","name":"%s"}' % (access_token, repository_name)

    response = requests.post(
        url=url,
        headers=header,
        data=request_body
    )

    print("Response code:", response.status_code)
    if response.status_code == 201:
        data_dict = json.loads(response.text)
        print("html链接: ", data_dict["html_url"])
        print("ssh链接: ", data_dict["ssh_url"])
    else:
        print(response.text)
